﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace IlianaRuler.CPL
{
    /// <summary>
    /// 与设备访问服务通讯的服务类
    /// </summary>
    class Communicator
    {
        /// <summary>
        /// 用于通讯的SOCKET
        /// </summary>
        private Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        /// <summary>
        /// 执行登录认证会话，登录过程是同步的
        /// </summary>
        /// <param name="customerId">用户ID</param>
        /// <param name="password">密码</param>
        private void Login(string customerId, string password)
        {
            LoginProtocol p = new LoginProtocol(customerId, password);
            byte[] buf = p.GetRequest();
        }

        /// <summary>
        /// 判断当前是否已通过连接和认证
        /// </summary>
        public bool IsConnected
        {
            get { return _socket.Connected; }
        }

        public Communicator()
        {

        }

        /// <summary>
        /// 登录到设备访问服务
        /// </summary>
        /// <param name="ipAddress">设备访问服务主机IP</param>
        /// <param name="port">端口号</param>
        /// <param name="customerId">登录的客户ID</param>
        /// <param name="password">登录的口令</param>
        public void Connect(string ipAddress, ushort port,
            string customerId, string password)
        {
            //重复打开前先关闭原有连接
            Close();
            //连接至远端主机
            _socket.Connect(ipAddress, port);

            //登录过程
            try
            {
                Login(customerId, password);
            }
            catch //登录失败关闭连接
            {
                Close();
                throw;
            }
        }

        public void Close()
        {
            if(_socket.Connected)
            {
                _socket.Shutdown(SocketShutdown.Both);
                _socket.Disconnect(true);
            }
        }

        public ErrorCode RegisterNode(byte nodeType, string serverIp,
            string nodeName, string neruonID, bool bindAlert)
        {
            return ErrorCode.Success;
        }

        public ErrorCode UpdateNode(byte nodeType, string serverIp,
            string nodeName, string neruonId)
        {
            return ErrorCode.Success;
        }

        public ErrorCode RemoveNode(byte nodeType, string serverIp,
            string nodeName)
        {
            return ErrorCode.Success;
        }

        public ErrorCode Timing(byte nodeType, string serverIp,
            string nodeName, TimingType timingType)
        {
            return ErrorCode.Success;
        }

        public ErrorCode SetTactics(byte nodeType, string serverIp,
            string nodeName, bool valid, TacticRule[] rules,
            string[] nodes)
        {
            return ErrorCode.Success;
        }

        public ErrorCode Control(byte nodeType, string serverIp,
            string nodeName, Single state)
        {
            return ErrorCode.Success;
        }

        public ErrorCode Noop()
        {
            return ErrorCode.Success;
        }

        public ErrorCode GetStatus(byte nodeType, string serverIp,
            string nodeName)
        {
            return ErrorCode.Success;
        }

        public ErrorCode SelfChecking(byte nodeType, string serverIp,
            string nodeName, SelfCheckingType selfCheckingType)
        {
            return ErrorCode.Success;
        }
    }
}
